#!/usr/bin/python
# -*- coding:utf-8 -*-

import Queue
import threading
import time
import sys
import os
import urllib
import re
import codecs
import logging

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys  # 导入模拟点击

from base import utils

reload(sys)
sys.setdefaultencoding("utf-8")


def getBrowser():
    # dcap = dict(DesiredCapabilities.PHANTOMJS)  #设置userAgent
    # dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36")
    browser = webdriver.PhantomJS(executable_path='C:\Python27\Scripts\phantomjs.exe')
    browser.implicitly_wait(30)  ##设置超时时间
    browser.set_page_load_timeout(30)  ##设置超时时间
    browser.maximize_window()  # 设置全屏
    return browser


def getGoodsId(goods_url):
    # https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.6baf272cieerQQ&id=541366140904&skuId=3413705479867&user_id=693060164&cat_id=50025174&is_b=1&rn=5d0f35dc26a7d746a49ca8b87b38667c
    # https://h5.m.taobao.com/awp/core/detail.htm?id=546774989823&wp_m=hotsell_goods_-1&wp_pk=shop/index_2167809912_65959343&from=inshop&wp_app=weapp
    end_pos = goods_url.find("h5.m.taobao.com")
    if end_pos == -1:
        end_pos = goods_url.find("detail.tmall.com")
        if end_pos == -1:
            return ''

    beg_targe = "id="
    id_beg = goods_url.find(beg_targe)
    if id_beg == -1:
        return ''

    id_beg += len(beg_targe)
    id_end = goods_url.find("&", id_beg)
    if id_end == -1:
        return ''

    return goods_url[id_beg:id_end]


def downCallBack(blocknum, blocksize, totalsize):
    '''回调函数
    @blocknum: 已经下载的数据块
    @blocksize: 数据块的大小
    @totalsize: 远程文件的大小
    '''
    # percent = 100.0 * blocknum * blocksize / totalsize
    # if percent > 100:
    #     percent = 100

    # print "download file %.2f%%"% percent
    pass


if __name__ == "__main__":
    utils.logConf(True, "taobao-" + utils.getSysTime() + ".log")

    goods_url = "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.6baf272cieerQQ&id=541366140904&skuId=3413705479867&user_id=693060164&cat_id=50025174&is_b=1&rn=5d0f35dc26a7d746a49ca8b87b38667c"
    # goods_url = "https://h5.m.taobao.com/awp/core/detail.htm?id=546774989823&wp_m=hotsell_goods_-1&wp_pk=shop/index_2167809912_65959343&from=inshop&wp_app=weapp"
    if len(sys.argv) > 1:
        goods_url = sys.argv[1]

    print 'goods_url=%s' % goods_url
    goods_id = getGoodsId(goods_url)
    goods_dir = goods_id + "/"
    goods_source_dir = goods_dir + "source/"
    goods_code_file = goods_dir + "code.html"
    goods_snapshot_file = goods_dir + "snapshot.png"
    if os.path.exists(goods_dir):
        os.rmdir(goods_dir)
    os.makedirs(goods_source_dir)

    try:
        browser = getBrowser()
        browser.get(goods_url)
        browser.save_screenshot(goods_snapshot_file)
        doc = open(goods_code_file, "w+")
        doc.write(browser.page_source)
        doc.close()

        # 获取资源文件
        tag_list = [('//link', 'href'), ('//script', 'src'), ('//img', 'src')]
        for tag_pair in tag_list:
            all_links = browser.find_elements_by_xpath(tag_pair[0])
            # print 'link element size = ', len(all_links)
            for link in all_links:
                source_url = link.get_attribute(tag_pair[1])
                if len(source_url) == 0:
                    # print 'tag = %s, attr = %s, source_url empty, not download' % (tag_pair[0], tag_pair[1])
                    continue

                # 以http或者//开始
                if source_url.find('http') != 0:
                    if source_url.find('//') != 0:
                        # print 'tag = %s, attr = %s, not begwith http or //, source_url = %s, not download' % (tag_pair[0], tag_pair[1], source_url)
                        continue

                # 如果中间有逗号隔开的是多个文件，不需要下载
                if source_url.find('.js,') != -1:
                    # print 'tag = %s, attr = %s, source_url = %s, is mul file, not download' % (tag_pair[0], tag_pair[1], source_url)
                    continue

                pos = source_url.find('.js&')
                if pos != -1:
                    source_url = source_url[0:pos + 3]
                    print 'new_source_url = %s' % source_url

                print 'tag = %s, attr = %s, source_url = %s' % (tag_pair[0], tag_pair[1], source_url)
                source_name = os.path.basename(source_url)
                # print 'source_name = %s' % source_name
                save_path = os.path.join(goods_id, source_name)
                if os.path.exists(save_path):
                    # print 'had exits %s' % save_path
                    continue

                    # print 'save_path = ', save_path
                    # download file
                    # urllib.urlretrieve(source_url, save_path, downCallBack)

    except Exception as e:
        print "[error]", e

    browser.quit()
